from typing import Sequence, Union
from common.constant import Exchange, Interval
from common.object import BarData, TickData
from .database import BaseDatabaseManager
from delphi import pythia
from datetime import datetime
from common.object import CTickBarData, CTickBarUnion, CBarData

class AmberDataManager(BaseDatabaseManager):
    def __init__(self) -> None:
        self.__client = pythia.AmberData.prod_client()
    
    def load_bar_data(self, symbol: str, exchange: Exchange, interval: Interval, start: datetime, end: datetime) -> Sequence[BarData]:
        data_set = "kline_{0}".format(str.lower(exchange.value))
        sub_section = [symbol]
        dst = pythia.new_ticket(data_set=data_set, sub_sections=sub_section, 
                                start=start.strftime('%Y-%m-%d %H:%M:%S'),
                                end = end.strftime('%Y-%m-%d %H:%M:%S'),
                                tags=["1m"])
        table = self.__client.get_data(dst)
        df = table.to_pandas()
        data = [CTickBarData(b"bar", CTickBarUnion(bar=CBarData(symbol.encode(), exchange.value.encode(), int(row[0].strftime("%Y%m%d")), Interval.MINUTE.value.encode(), row[1], 0, row[2], row[4], row[5], row[3], 0, row[0].timestamp()/1000.0, symbol.encode(), b"DB"))) \
                for row in df[["timestamp_in_micro", "vol",  "open", "close", "high", "low", "trade_num"]].values]
        return data

    def load_tick_data(self, symbol: str, exchange: Exchange, start: datetime, end: datetime) -> Sequence[TickData]:
        print("not support")
        return None

    def save_bar_data(self, datas: Sequence[BarData]):
        return None
    def save_tick_data(self, datas: Sequence[TickData]):
        return None
    def get_newest_bar_data(self, symbol: str, exchange: Exchange, interval: Interval) -> Union[BarData, None]:
        return None
    def get_newest_tick_data(self, symbol: str, exchange: Exchange) ->  Union[TickData, None]:
        return None
    def clean(self, symbol: str):
        print("clean {} data".format(symbol))
        return None
    